package task

import (
	"GoPro/Server/msgProto"
	"GoPro/Server/mynet"
	"GoPro/Server/protoMsg"
	"encoding/binary"
	"google.golang.org/protobuf/proto"
)

type JoinRoomRouter struct {
}

func (msg *JoinRoomRouter) PreHandler(req mynet.IRequest) bool {
	_, ok := req.GetServer().OnlineUser[req.GetIConnection()]
	if ok {
		header := msgProto.MsgHeader{}
		header.MsgType = 400
		header.MsgSize = 0
		binary.Write(req.GetIConnection().GetWriter(), binary.BigEndian, header)
		return false
	}
	return true
}

func (msg *JoinRoomRouter) Handler(req mynet.IRequest) bool {
	header := msgProto.MsgHeader{}
	header.MsgType = 400
	header.MsgSize = 0

	data := req.GetData()
	joinMsg := protoMsg.JoinRoom{}
	proto.Unmarshal(data, &joinMsg)
	if joinMsg.GetRoomNumber() != "" {
		header.MsgType = 200
		req.GetServer().ManagerRoom.UserJoin(joinMsg.GetRoomNumber(), req.GetServer().OnlineUser[req.GetIConnection()], req.GetIConnection())
	}
	binary.Write(req.GetIConnection().GetWriter(), binary.BigEndian, header)
	return true
}

func (msg *JoinRoomRouter) PostHandler(req mynet.IRequest) bool {
	return true
}
